<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/10
 * Time: 20:21
 */

namespace App\HttpController\V1;

use App\Common\Checking;
class Address extends Auth
{
    protected $user_id;
    //收货地址列表
    public function index(){

        $this->user_id = $this->user->id;
        $address = $this->db->where('user_id',$this->user_id)->get('address',null,'*');
        if($address){
            foreach ($address as $k=>$v){
                $address[$k]['address_info'] = $v['province'].' '.$v['city'].' '.$v['area'].' '.$v['address'];
            }
        }
        $this->writeJson( 200, $address, '获取成功' );
    }

    /*
     * 添加收货地址
     */
    public function add(){
        $this->user_id = $this->user->id;
        $param = $this->input('name,phone,address_info,address');
        try {
            if (!Checking::phoneRule($param['phone'])) {
                throw new \Exception( Checking::MESSAGE['phoneRule'],-201);
            }
            if(empty($param['name'])){
                throw new \Exception('收货人姓名不能为空',-202);
            }
            if(empty($param['address_info'])|| empty($param['address'])){
                throw new \Exception('收货地址不能为空',-203);
            }
            $has=$this->db->where('user_id',$this->user_id)->has('address');
            if(!$has){
                $params['is_default'] =2;
            }else{
                $params['is_default'] =1;
            }
            $address = explode(';',$param['address_info']);
            $params['user_id'] = $this->user_id;
            $params['province'] = $address[0];
            $params['city'] = $address[1];
            $params['area'] = $address[2];
            $params['address'] = $param['address'];
            $params['name'] = $param['name'];
            $params['phone'] = $param['phone'];
            $params['create_time'] = date('Y-m-d H:i:s',time());
            $info = $this->db->insert('address',$params);
            if(!$info){
                throw new \Exception('添加地址失败',-204);
            }
            throw new \Exception('添加地址成功',200);
        }catch (\Exception $e){
            $this->writeJson( $e->getCode(), null, $e->getMessage() );
        }

    }

    /*
     * 编辑收货地址
     */
    public function edit(){
        $this->user_id = $this->user->id;
        $param = $this->input('id,name,phone,address_info,address');
        try {
            if (!Checking::phoneRule($param['phone'])) {
                throw new \Exception( Checking::MESSAGE['phoneRule'],-201);
            }
            if(empty($param['id'])){
                throw new \Exception('参数错误',-202);
            }
            if(empty($param['name'])){
                throw new \Exception('收货人姓名不能为空',-203);
            }
            if(empty($param['address_info'])|| empty($param['address'])){
                throw new \Exception('收货地址不能为空',-204);
            }
            $address = explode(';',$param['address_info']);
            $params['province'] = $address[0];
            $params['city'] = $address[1];
            $params['area'] = $address[2];
            $params['address'] = $param['address'];
            $params['name'] = $param['name'];
            $params['phone'] = $param['phone'];
            $params['create_time'] = date('Y-m-d H:i:s');
            $info = $this->db->where('id',$param['id'])->update('address',$params);
            if(!$info){
                throw new \Exception('编辑地址失败',-205);
            }
            throw new \Exception('编辑地址成功',200);
        }catch (\Exception $e){
            $this->writeJson( $e->getCode(), null, $e->getMessage() );
        }

    }

    /*
     * 删除收货地址
     */
    public function del(){
        $this->user_id = $this->user->id;
        $id = $this->input('id');
        try{
            if(empty($id)){
                throw new \Exception('参数不能为空',-201);
            }
            $info = $this->db->where('id',$id)->getOne('address','id,name,phone,is_default');
            if(!$info){
                throw new \Exception('该收货地址不存在',-202);
            }
            $infos = $this->db->where('id',$id)->delete('address',1);
            if($infos){
                throw new \Exception('删除地址成功',200);
            }
        }catch (\Exception $e){
            $this->writeJson( $e->getCode(), null, $e->getMessage() );
        }

    }
    /*
     * 设置默认地址
     */
    public function default(){
        $this->user_id = $this->user->id;
        $param = $this->input('id,default');
        try{
            if($param['default'] != 2){
                throw new \Exception('参数错误',-201);
            }
            if(empty($param['id'])){
                throw new \Exception('参数不能为空',-202);
            }
            $add = $this->db->where('id',$param['id'])->getOne('address','id');
            if(!$add){
                throw new \Exception('地址不存在',-203);
            }
            $address = $this->db->where('user_id',$this->user_id)->update('address',['is_default'=>1]);
            if($address){
                $infos = $this->db->where('user_id',$this->user_id)->where('id',$param['id'])->update('address',['is_default'=>$param['default']]);
                if(!$infos){
                    throw new \Exception('修改失败',-204);
                }
                throw new \Exception('修改成功',200);
            }
        }catch (\Exception $e){
            $this->writeJson($e->getCode(),null,$e->getMessage());
        }

    }

    //收货地址详情
    public function list(){
        $this->user_id = $this->user->id;
        $param = $this->input('id');
        try{
            if(empty($param)){
                throw new \Exception('参数不能为空',-201);
            }
            $add = $this->db->where('id',$param)->where('user_id',$this->user_id)->getOne('address','id,province,city,area,address,phone,name');
            return  $this->writeJson(200,$add,'成功');
        }catch (\Exception $e){
            $this->writeJson($e->getCode(),null,$e->getMessage());
        }

    }

    //选择收货地址
    public function sele()
    {
        $id = $this->input('id');
        try{
            if(empty($id)){
                throw new \Exception('参数不能为空',-201);
            }
            $add = $this->db->where('id',$id)->where('user_id',$this->user->id)->getOne('address','id,province,city,area,address,phone,name');
            if($add){
                $this->db->where('user_id',$this->user->id)->update('address',['is_default'=>1]);
                $this->db->where('user_id',$this->user->id)->where('id',$id)->update('address',['is_default'=>2]);
            }
            return  $this->writeJson(200,$add,'成功');
        }catch (\Exception $e){
            $this->writeJson($e->getCode(),null,$e->getMessage());
        }
    }
}